package com.saltlux.storm.study.bbs.impl.h2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.saltlux.storm.study.bbs.ArticleDAO;
import com.saltlux.storm.study.bbs.ArticleDTO;

public class ArticleDAOImpl implements ArticleDAO {
	public ArticleDAOImpl() {
		boolean ensureTable = false;
		ensureTable = checkTable();
		if (!ensureTable) {
			createArticleTable();
			ensureTable = true;
		}
	}

	private boolean checkTable() {
		boolean ensureTable = false;
		List<Map<String, String>> rs = DatabaseHelper.executeQuery("show tables");

		for (Map<String, String> eachMap : rs) {
			String tableName = eachMap.get("TABLE_NAME");
			if (tableName.equalsIgnoreCase("articles")) {
				ensureTable = true;
				break;
			}
		}
		return ensureTable;
	}

	private boolean createArticleTable() {
		return DatabaseHelper
				.execute("create table articles (id IDENTITY, boardId VARCHAR(100), subject  VARCHAR(100), content VARCHAR(2000), createdAt VARCHAR(100), createdBy VARCHAR(100), lastModifiedAt VARCHAR(100), lastModifiedBy VARCHAR(100), hits VARCHAR(100))");
	}

	public void add(ArticleDTO articleDTO) {
		String queryString = "insert into articles (boardId, subject, content, createdAt , createdBy, lastModifiedAt , lastModifiedBy , hits)" + " values ('"
				+ articleDTO.getBoardId() + "', '" + articleDTO.getSubject() + "', '" + articleDTO.getContent() + "','" + articleDTO.getCreatedAt() + "', '"
				+ articleDTO.getCreatedBy() + "' , '" + articleDTO.getLastModifiedAt() + "', '" + articleDTO.getLastModifiedBy() + "'," + articleDTO.getHits()
				+ ")";
		Long id = DatabaseHelper.executeInsertSingle(queryString);
		articleDTO.setId(id.toString());
	}

	public void remove(String id) {
		DatabaseHelper.execute("delete from articles where id = " + id);
	}

	public List<ArticleDTO> list(String boardId, int startPosition, int returnCount) {
		List<Map<String, String>> list = DatabaseHelper.executeQuery("SELECT * FROM ARTICLES WHERE BOARDID = " + boardId);
		List<ArticleDTO> articles = new ArrayList<ArticleDTO>();

		for (Map<String, String> eachMap : list) {
			articles.add(ArticleDTO.valueOf(eachMap));
		}

		return articles;
	}

	public ArticleDTO get(String id) {
		List<Map<String, String>> list = DatabaseHelper.executeQuery("SELECT * FROM ARTICLES WHERE ID=" + id);
		if (list.size() != 1) {
			return null;
		}
		Map<String, String> map = list.get(0);

		return ArticleDTO.valueOf(map);
	}

	public void modify(ArticleDTO articleDTO) {
		String queryString = "UPDATE ARTICLES SET SUBJECT = '" + articleDTO.getSubject() + "', CONTENT = '" + articleDTO.getContent() + "', LASTMODIFIEDAT = '"
				+ articleDTO.getLastModifiedAt() + "', LASTMODIFIEDBY = '" + articleDTO.getLastModifiedBy() + "', HITS = '" + articleDTO.getHits()
				+ "', CREATEDAT = '" + articleDTO.getCreatedAt() + "', CREATEDBY = '" + articleDTO.getCreatedBy() + "', BOARDID='" + articleDTO.getBoardId()
				+ "' WHERE ID = " + articleDTO.getId();
		DatabaseHelper.execute(queryString);
	}

}
